package com.handa.common.exception;


import com.handa.common.entity.Response;
import com.handa.common.enums.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static com.handa.common.enums.ResponseCode.BAD_REQUEST;


@RestControllerAdvice
@Slf4j
public class ExceptionAdvice {


    @ExceptionHandler(IllegalArgumentException.class)
    public Response<?> handlerIllegalArgumentException(IllegalArgumentException e) {
        log.error("参数非法：", e);
        return Response.error(BAD_REQUEST, e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Response<?> handlerException(Exception e) {
        log.error("", e);
        return Response.error(ResponseCode.INTERNAL_SERVER_ERROR, e.getMessage());
    }

    @ExceptionHandler(ServiceException.class)
    public Response<?> handlerServiceException(ServiceException e) {
        log.error("", e);
        return Response.error(ResponseCode.INTERNAL_SERVER_ERROR, e.getMessage());
    }

    @ExceptionHandler(BadCredentialsException.class)
    public Response<?> handleBadCredentialsException(BadCredentialsException e) {
        log.error("", e);
        return Response.error(ResponseCode.INTERNAL_SERVER_ERROR, e.getMessage());
    }
}
